package cn.mysource.org.controller;

import cn.mysource.basic.util.AjaxResult;
import cn.mysource.basic.util.PageList;
import cn.mysource.org.domain.Department;
import cn.mysource.org.domain.Employee;
import cn.mysource.org.domain.Permission;
import cn.mysource.org.domain.Shop;
import cn.mysource.org.query.EmployeeQuery;
import cn.mysource.org.query.PermissionQuery;
import cn.mysource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询详情")
    public Employee findById(@ApiParam(value = "部门id", required = true) @PathVariable("id") Long id) {
        return employeeService.findById(id);

    }



    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null != employee.getId()) {
            // 修改
            employeeService.update(employee);
        } else {
            // 添加
            employeeService.add(employee);
        }
        return AjaxResult.me().success();
    }


    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }


    @PostMapping()
    public PageList<Employee> queryByPage(@RequestBody PermissionQuery query) {
        PageList<Employee> list = employeeService.queryByPage(query);
        return list;
    }


    @GetMapping()
    public List<Employee> findAll() {
        return employeeService.findAll();
    }



    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }


    @GetMapping("/dept")
    public List<Department> findDeptAll(){
        return employeeService.findDeptAll();
    }

    @GetMapping("/shop")
    public List<Shop> findShopAll(){
        return employeeService.findShopAll();
    }

    @GetMapping("/findByEmp/{id}")
    @ApiOperation(value = "通过ID查询详情")
    public List<Employee> findByEmp(@ApiParam(value = "部门id", required = true) @PathVariable("id") Long id) {
        List<Employee> findByEmp = employeeService.findByEmp(id);
        return findByEmp;
    }



}
